#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>

const int SCR_WIDTH = 800;
const int SCR_HEIGHT = 800;

struct MyGLContext
{
    GLFWwindow *window;
    unsigned int vao;
    unsigned int vbo;
    unsigned int shader_program;

    MyGLContext();

    void create_window();
    void prepare_data();
    void run();

    int is_valid() { return window != NULL; }
};

MyGLContext::MyGLContext()
{
    window = NULL;
    create_window();
    prepare_data();
}

void MyGLContext::create_window()
{
    window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Hello Triangle", NULL, NULL);
    if (window == NULL)
    {
        std::cout << "Failed to create glfw window" << std::endl;
        glfwTerminate();
    }
}

void init_gl_environ();

int main()
{
    init_gl_environ();

    MyGLContext gl_context;
    if (!gl_context.is_valid())
    {
        return -1;
    }

    gl_context.run();

    glfwTerminate();
    return 0;
}

void init_gl_environ()
{
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed  to initialize GLAD" << std::endl;
        return;
    }
}
